package com.ikkong.platform.controller;

import com.ikkong.core.base.BaseController;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.toolbox.Func;
import com.ikkong.core.toolbox.Record;
import com.ikkong.platform.meta.intercept.BlogIntercept;
import com.ikkong.platform.model.Blog;
import com.ikkong.platform.service.BlogService;
import com.ikkong.platform.service.impl.BlogServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.system.utils.QiniuUtil;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.FileKit;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import org.beetl.sql.core.kit.StringKit;

import javax.xml.ws.Action;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * Generated by Blade.
 * 2016-10-08 15:59:45
 */

public class BlogController extends UrlPermissController {
	private static String CODE = "blog";
	private static String PERFIX = "tb_tfw_blog";
	private static String LIST_SOURCE = "Blog.list";
	private static String BASE_PATH = "/platform/blog/";
	
	BlogService service = new BlogServiceImpl();

	public void index() {

		setAttr("code", CODE);
		render(BASE_PATH + "blog.html");
	}

	@ClearShiro
	public void ueditor() throws IOException {
		// 百度编辑器加载出按钮图标前 会将所有控件的路径 先通过config.json
		// 文件加载出来(包括上传图片路径，视频路径等路径都是通过config.json 文件读取的)
		// 所以某些控件点击不了 是因为 config.json文件没有找到 或者是文件里面的路径有问题
		String action = getPara("action");
		if ("config".equals(action)) {
			// 这里千万注意 "config.json" 文件前方的目录一定要正确
			render("/qiniu.ueditor.config.json");//这里地址写成自己的config.json所在的地址
			return;
		}
		// "upfile" 来自 config.json 中的 imageFieldName 配置项
		UploadFile uf = getFile("upfile");
		File file = uf.getFile();

		Record res = QiniuUtil.uploadQiniu(file, UUID.randomUUID().toString());
		String url = "";
		if(Func.toInt(res.get("isOK"))==1){
			url = res.get("key").toString();
			try {
				FileKit.delete(file);
			}catch (Exception e){
				System.out.println(e);
			}
		}else {
			url = "/upload/"+ uf.getFileName();
		}
		System.out.println(url);
		String fileName = uf.getFileName();
		String[] typeArr = fileName.split("\\.");
		Ret ret = Ret.create("state", "SUCCESS")
				.set("url", url)//文件上传地址
//				.set("file",uf)
				.set("title", url)
				.set("original", uf.getOriginalFileName())
				.set("type", "." + typeArr[1]) // 这里根据实际扩展名去写
				.set("size", uf.getFile().length());
		renderJson(ret);
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "blog_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Blog blog = service.findById(id);
		setAttr("model", JsonKit.toJson(blog));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "blog_edit.html");
	}

	@Action
	public void test(){
		renderText("222");
//		List keys = CacheKit.getKeys("shiro-activeSessionCache");
//		CacheKit.removeAll("shiro-activeSessionCache");
//
//
//		HttpSession session = getSession();
//		Enumeration<String> attributeNames = session.getAttributeNames();
//		String id = session.getId();
//		Object jsessionid = session.getAttribute("JSESSIONID");
//		SessionKit sessionKit = new SessionKit();
//		renderJson(json("hello"));
	}

	public void view() {
		String id = getPara(0);
		Blog blog = service.findById(id);
		setAttr("model", JsonKit.toJson(blog));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "blog_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new BlogIntercept());
		renderJson(grid);
	}

	public void save() {
		Blog blog = mapping(PERFIX, Blog.class);
		boolean temp = service.save(blog);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Blog blog = mapping(PERFIX, Blog.class);
		blog.setVersion(blog.getVersion()+1);
		boolean temp = service.update(blog);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
